package com.arpa.ntocc.basic.api.controller;

import cn.hutool.core.bean.BeanUtil;
import com.arpa.core.web.MediaTypes;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.controller.BaseController;
import com.arpa.ntocc.common.domain.dto.ResourceDTO;
import com.arpa.ntocc.common.domain.entity.Resource;
import com.arpa.ntocc.common.domain.vo.ResourceVO;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.ntocc.common.security.annotation.NoRepeat;
import com.arpa.ntocc.common.service.IResourceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 资源 Controller
 * </p>
 *
 * @author LEO
 * @since 2019-12-16
 */
@Api(tags = "资源")
@RestController
@RequestMapping(value="/resource", produces = "application/json;charset=UTF-8")
@Log4j2
public class ResourceController extends BaseController {

   @Autowired
   private IResourceService  iResourceService;

    /**
     * 获取资源树
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "获取资源树", notes = "获取资源树")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = Resource.class))
    @NeedLogin
    @GetMapping(value = "/tree")
    public Result resource(HttpServletRequest request) {
        try {
            String branchCode = UserUtil.getBranchCode();
            List<ResourceVO> treeData = iResourceService.treeData(branchCode);
            return Result.ok().data(treeData);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加 资源
     *
     * @param entity Resource
     * @param request
     * @return
     */
    @ApiOperation(value = "添加 资源 ", notes = "添加 资源 ")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @NoRepeat(time = 2000)
    @PostMapping()
    public Result create(@Valid ResourceDTO entity, HttpServletRequest request) {
        try {

            Resource resource = BeanUtil.toBean(entity,Resource.class);
            resource.setBranchCode(UserUtil.getBranchCode());
            iResourceService.save(resource);
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
    * 获取资源详情
    *
    * @param code
    * @param request
    * @return
    */
    @ApiOperation(value = "获取资源详情", notes = "获取资源详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "code", value = "资源code", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = Resource.class))
    @NeedLogin
    @GetMapping(value = "/{code}")
    public Result resource(@PathVariable String code, HttpServletRequest request) {
        try {
            Resource entity = iResourceService.getOne(new QueryWrapper<Resource>().lambda().eq(Resource::getCode,code));
            return Result.ok().data(entity);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改资源
     *
     * @param entity 资源
     * @param request
     * @return
     */
    @ApiOperation(value = "修改资源", notes = "修改资源")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping()
    public Result update(@Valid ResourceDTO entity, HttpServletRequest request) {
        if(StringUtils.isBlank(entity.getCode())){
            return Result.error("code不能为空");
        }
        Resource resource = BeanUtil.toBean(entity,Resource.class);
        int updateNum = iResourceService.updateByCode(resource);
        if(updateNum == 0){
            return Result.error("没有更新数据！code:"+entity.getCode());
        }else{
            return Result.ok("修改成功,更新"+updateNum+"条数据！");
        }
    }

    /**
     * 删除 资源
     *
     * @param codes
     * @param request
     * @return
     */
    @ApiOperation(value = "删除 资源 ", notes = "删除 资源 ", produces = MediaTypes.JSON_UTF_8)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "codes", value = "codes", required = true, paramType = "query", dataType = "List")
    })
    @NeedLogin
    @DeleteMapping(value = "")
    public Result batchdel(@RequestParam List<String> codes, HttpServletRequest request) {
        try {
            iResourceService.remove(new QueryWrapper<Resource>().lambda().in(Resource::getCode,codes));
            return Result.ok("操作成功！");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

 }
